package com.crazyhat.crazyhat.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crazyhat.crazyhat.dto.CommentDTO;
import com.crazyhat.crazyhat.dto.PostAndCommentInfoDTO;
import com.crazyhat.crazyhat.entity.Comment;
import com.crazyhat.crazyhat.pojo.Result;
import com.crazyhat.crazyhat.service.ICommentService;



/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author crazyhat
 * @since 2024-07-05
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    /**
     * 获取动态的评论列表，分页
     * @param postId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/commentListByPage")
    public Result<Page<CommentDTO>> getCommentListByPage(
            @RequestParam(value = "postId") Integer postId,
            @RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize) {

        Page<CommentDTO> commentList=commentService.getCommentListByPage(postId,pageNum,pageSize);

        return Result.success(commentList);
    }

    @GetMapping("/commentList")
    public Result<List<CommentDTO>> getPostList(@RequestParam(value = "postId") Integer postId) {

        List<CommentDTO> commentList=commentService.getCommentList(postId);

        return Result.success(commentList);
    }


    /**
     * 获取动态及其评论列表
     * @param postId
     * @return
     */
    @GetMapping("/postAndCommentInfo")
    public Result getPostAndCommentInfo(@RequestParam(value = "postId") Integer postId) {

        PostAndCommentInfoDTO postAndCommentInfo=commentService.getPostAndCommentInfo(postId);

        if (postAndCommentInfo!=null) {
            return Result.success(postAndCommentInfo);
        } else {
            return Result.error("获取动态及评论列表失败");
        }
    }

    /**
     *
     * @param commentId
     * @return
     */

    /**
     * 删除评论
     * @param commentId
     * @return
     */
    @DeleteMapping("/deleteComment")
    public Result deleteComment(@RequestParam(value = "commentId") Integer commentId){

        boolean isSuccess=commentService.deleteComment(commentId);

        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error("删除评论失败");
        }
    }


    /**
     * 发布评论
     * @param comment
     * @return
     */
    @PostMapping("/createComment")
    public Result createComment(@RequestBody @Validated Comment comment) {

        Comment newComment=commentService.createComment(comment);

        if (newComment!=null) {
            return Result.success(newComment);
        } else {
            return Result.error("发布评论失败");
        }
    }

}
